package com.ky.user.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.common.vo.R;
import com.ky.user.entity.Post;
import com.ky.user.entity.User;
import com.ky.user.mapper.PostMapper;
import com.ky.user.mapper.UserMapper;
import com.ky.user.service.PostService;
import com.ky.user.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hmt
 * @since 2024-06-17
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
   @Autowired
    private PostMapper postMapper;
   @Autowired
   private UserMapper userMapper;
    @Override
    public R findAll() {
        ArrayList<PostVo> postVos = new ArrayList<>();
        List<Post> postList = postMapper.selectList(null);
        for (Post post : postList) {
            PostVo postVo = new PostVo();
            Integer userId = post.getUserId();
            User user = userMapper.selectById(userId);
            postVo.setUsername(user.getUsername());
          postVo.setContent(post.getContent());
          postVo.setTimestamp(post.getTimestamp());
          postVo.setImageUrl(post.getImageUrl());
          postVo.setCommentsCount(post.getCommentsCount());
          postVo.setLikesCount(post.getLikesCount());

            postVos.add(postVo);
        }
        return R.ok(postVos);
    }
}
